package jp.co.sakaiever.tool.pages.windows;


import org.apache.commons.lang.StringUtils;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxFallbackButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import jp.co.sakaiever.logic.ExternalLogic;
import jp.co.sakaiever.logic.SakaieverLogic;
import jp.co.sakaiever.model.SakaieverItem;
import jp.co.sakaiever.tool.Locator;
import jp.co.sakaiever.tool.components.FocusOnLoadBehaviour;

public class AddEditItem extends Panel {

	private static final long serialVersionUID = 1L;
	private boolean addMode = true;
	
	public AddEditItem(String id, final ModalWindow window, SakaieverItem item){
        super(id);

        //window setup
		window.setTitle(new ResourceModel("item.add")); 
		window.setInitialHeight(150);
		window.setInitialWidth(350);
		window.setResizable(false);
		
		//edit or new?
		if(item == null) {
			item = new SakaieverItem();
		} else {
			addMode = false;
		}
		
		Form form = new Form("form", new Model(item));
		
		//feedback
		final Label feedback = new Label("feedback");
		feedback.setOutputMarkupPlaceholderTag(true);
		form.add(feedback);
		
		//title text
		Label titleText = new Label("item-title-text", new ResourceModel("item_add.title"));
		form.add(titleText);
		
		//title field
		TextField title = new TextField("item-title", new PropertyModel(item, "title"));
		title.add(new FocusOnLoadBehaviour());
		form.add(title);
		
		//hidden text
		Label hiddenText = new Label("item-hidden-text", new ResourceModel("item_add.hidden"));
		form.add(hiddenText);
		
		//hidden checkbox
		CheckBox hidden = new CheckBox("item-hidden", new PropertyModel(item, "hidden"));
		form.add(hidden);
		
		//submit button
		AjaxFallbackButton submit = new AjaxFallbackButton("submit", new ResourceModel("button.save"), form) {
			private static final long serialVersionUID = 1L;

			protected void onSubmit(AjaxRequestTarget target, Form form) {
				
				//get form model
				SakaieverItem item = (SakaieverItem)form.getModelObject();
				
				//validate title
				if(StringUtils.isBlank(item.getTitle())) {
					feedback.setModel(new ResourceModel("title_required"));
					feedback.add(new AttributeModifier("class", true, new Model("alertMessage")));
					target.addComponent(feedback);
					return;
				}
				
				//set other parts of the item
				if(addMode) {
					item.setLocationId(getExternalLogic().getCurrentLocationId());
					item.setOwnerId(getExternalLogic().getCurrentUserId());
				}
				
				//save it and close window
				getSakaieverLogic().saveItem(item);
				window.close(target);
				
            }
		};
		form.add(submit);
		
		//for editing, update the labels with the appropriate values
		if(!addMode) {
			window.setTitle(new ResourceModel("item.edit")); 
			submit.setLabel(new ResourceModel("button.edit"));
		}
		
        
        //add form
        add(form);
    }

	protected SakaieverLogic getSakaieverLogic()
    {
    	return Locator.getSakaieverLogic();
    }
	
	protected ExternalLogic getExternalLogic()
    {
    	return Locator.getExternalLogic();
    }
}



